package com.movie.service.impl;

import com.movie.dao.ScreeningMapper;
import com.movie.model.Screening;
import com.movie.service.ScreeningService;
import com.movie.util.ScreeningRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class ScreeningServiceImpl implements ScreeningService {

    @Autowired
    private ScreeningMapper screeningMapper;

    @Override
    public List<Screening> getScreeningsByMovie(Integer movieId) {
        return screeningMapper.getScreeningsByMovie(movieId);
    }

    @Override
    @Transactional
    public String addScreening(ScreeningRequest request) {
        try {
            // 验证输入数据
            if (request.getMovieId() == null || request.getHallId() == null || 
                request.getScreeningTime() == null || request.getPrice() == null) {
                return "error: 请填写完整的场次信息";
            }

            // 检查放映时间是否在未来
            if (request.getScreeningTime().before(new Date())) {
                return "error: 放映时间必须在当前时间之后";
            }

            // 检查场次冲突
            if (screeningMapper.checkScreeningConflict(request.getHallId(), request.getScreeningTime(), null)) {
                return "error: 该时间段影厅已有其他场次安排";
            }

            // 创建场次对象
            Screening screening = new Screening();
            screening.setMovieId(request.getMovieId());
            screening.setHallId(request.getHallId());
            screening.setScreeningTime(request.getScreeningTime());
            screening.setPrice(request.getPrice());

            // 保存到数据库
            screeningMapper.addScreening(screening);
            return "success";
        } catch (Exception e) {
            e.printStackTrace();
            return "error: 添加场次失败";
        }
    }

    @Override
    public List<Screening> getAllScreenings() {
        return screeningMapper.getAllScreenings();
    }

    @Override
    public Screening getScreeningById(Integer id) {
        return screeningMapper.getScreeningById(id);
    }

    @Override
    @Transactional
    public String updateScreening(Integer id, ScreeningRequest request) {
        try {
            // 验证输入数据
            if (request.getMovieId() == null || request.getHallId() == null || 
                request.getScreeningTime() == null || request.getPrice() == null) {
                return "error: 请填写完整的场次信息";
            }

            // 检查放映时间是否在未来
            if (request.getScreeningTime().before(new Date())) {
                return "error: 放映时间必须在当前时间之后";
            }

            // 检查场次冲突（排除当前场次）
            if (screeningMapper.checkScreeningConflict(request.getHallId(), request.getScreeningTime(), id)) {
                return "error: 该时间段影厅已有其他场次安排";
            }

            // 更新场次对象
            Screening screening = new Screening();
            screening.setId(id);
            screening.setMovieId(request.getMovieId());
            screening.setHallId(request.getHallId());
            screening.setScreeningTime(request.getScreeningTime());
            screening.setPrice(request.getPrice());

            // 更新数据库
            screeningMapper.updateScreening(screening);
            return "success";
        } catch (Exception e) {
            e.printStackTrace();
            return "error: 更新场次失败";
        }
    }

    @Override
    @Transactional
    public String deleteScreening(Integer id) {
        try {
            screeningMapper.deleteScreening(id);
            return "success";
        } catch (Exception e) {
            e.printStackTrace();
            return "error: 删除场次失败";
        }
    }
}
